package com.danskin.wechat.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.danskin.wechat.cache.JedisClient;
import com.danskin.wechat.dao.SysAreaDao;
import com.danskin.wechat.entity.SysArea;
import com.danskin.wechat.entity.example.SysAreaExample;
import com.danskin.wechat.entity.example.SysAreaExample.Criteria;
import com.danskin.wechat.service.SysAreaService;
import com.danskin.wechat.util.JsonUtils;
@Service
public class SysAreaServiceImpl extends BaseServiceImpl implements SysAreaService {

	@Autowired
	private JedisClient jedisClient;
	@Autowired
	private SysAreaDao  areaDao;
	

	

	

	@Override
	public List<SysArea> getSysAllAreaList() {
		Map<String,String> map=new HashMap<String, String>();
		Map<String,String> map2=new TreeMap<String, String>();
	/*	try {
			String json=	jedisClient.get(STANDAR_AREA_MAP);
			if(!StringUtils.isBlank(json)){
				//JSON转换为pojo
				List<SysArea> jsonToList = JsonUtils.jsonToList(json, SysArea.class);
				return jsonToList;
			}
			} catch (Exception e) {
				e.printStackTrace();
			}*/
		SysAreaExample example=new SysAreaExample();
		Criteria criteria = example.createCriteria();
		criteria.andCodeIsNotNull();
		List<SysArea> list = areaDao.selectByExample(example);
		for (SysArea sysArea : list) {
			map.put(sysArea.getCode(), sysArea.getName());
			map2.put(sysArea.getMdmCode(), sysArea.getCode());
		}
		 jedisClient.set(STANDAR_AREA_MAP, JsonUtils.objectToJson(map));
		 jedisClient.set(MDMCODE_STANDAER_MAP, JsonUtils.objectToJson(map2));
		return list;
	}
	@Override
	public String getSysAreaByCode(String Code) {
		if(!this.SysAreaMap.isEmpty()){
			return SysAreaMap.get(Code);
		}else{
			Map<String, String> map = this.getSysAreaMap();
			if(!map.isEmpty()){
				return map.get(Code);
			}else{
				SysAreaExample example=new SysAreaExample();
				Criteria criteria = example.createCriteria();
				criteria.andCodeEqualTo(Code);
				List<SysArea> list = areaDao.selectByExample(example);
				if(list.size()>0){
					return list.get(0).getName();
				}else{
					return null;
				}
			}
		}
		
	}
	@Override
	public String getStandarCodeByMDMCode(String MDMCode) {
		if(!this.MDMCodeStandarCodeMap.isEmpty()){
			return MDMCodeStandarCodeMap.get(MDMCode);
		}else{
			Map<String, String> map = this.getMDMCodeAndStandarCodeMap();
			if(!map.isEmpty()){
				return map.get(MDMCode);
			}else{
				SysAreaExample example=new SysAreaExample();
				Criteria criteria = example.createCriteria();
				criteria.andMdmCodeEqualTo(MDMCode);
				List<SysArea> list = areaDao.selectByExample(example);
				if(list.size()>0){
					return list.get(0).getName();
				}else{
					return null;
				}
			}
		}
	}

}
